package com.apes.scm.masterdata.party.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.area.model.Area;
import com.apes.scm.masterdata.area.repository.AreaRepository;
import com.apes.scm.masterdata.party.model.SupplierReach;
import com.apes.scm.masterdata.party.model.SupplierShip;
import com.apes.scm.masterdata.party.repository.SupplierReachRepository;
import com.apes.scm.masterdata.party.repository.SupplierShipRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 功能：供应商信息
 *
 * @author xul
 * @create 2018-01-23 14:35
 */
@Service("supplierReachService")
public class SupplierReachService extends DomainService {
    @Autowired
    SupplierReachRepository supplierReachRepository;
    @Autowired
    CorporateSerive corporateSerive;
    @Autowired
    AreaRepository areaRepository;
    public SupplierReach save(SimpleRequest request) {

        SupplierReach supplierReach = request.getO(SupplierReach.class);
        // 设置注册省份、注册城市、办公省份、办公城市
        // 获取注册区县
        Area regCounty = supplierReach.getCounty();// 注册地址.区县
        // 获取注册城市编码
        String regCityId = regCounty.getParent();
        // 查找注册城市
        Area regCity = areaRepository.findOne(regCityId);
        supplierReach.setCity(regCity); // 注册地址.城市
        // 设置注册城市
        String regProvinceId = regCity.getParent();
        Area regProvince = areaRepository.findOne(regProvinceId);
        supplierReach.setProvince(regProvince); //注册地址.省份
        supplierReach.setState("create");
        return supplierReachRepository.saveAndFlush(supplierReach);
    }


    // 更新供应商信息
    public SupplierReach update(SimpleRequest request) {

        SupplierReach supplierReach = request.getO(SupplierReach.class);
        // 设置注册省份、注册城市、办公省份、办公城市
        // 获取注册区县
        Area regCounty = supplierReach.getCounty();// 注册地址.区县
        // 获取注册城市编码
        String regCityId = regCounty.getParent();
        // 查找注册城市
        Area regCity = areaRepository.findOne(regCityId);
        supplierReach.setCity(regCity); // 注册地址.城市
        // 设置注册城市
        String regProvinceId = regCity.getParent();
        Area regProvince = areaRepository.findOne(regProvinceId);
        supplierReach.setProvince(regProvince); //注册地址.省份
        supplierReach.setState("create");
        return supplierReachRepository.saveAndFlush(supplierReach);
    }
    public SupplierReach delete(SimpleRequest request) {
        String id = request.get("id");
        Integer versions = request.get("version");
        SupplierReach supplierReach = supplierReachRepository.findOne(Long.valueOf(id));
        if (supplierReach == null) throw new RuntimeException("供应商送达仓库【" + id + "】在系统中不存在。");
        if (supplierReach.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        supplierReach.setState("delete");
        return supplierReachRepository.saveAndFlush(supplierReach);
    }

}
